package com.budwk.app.sqfwV2.generator.patrolAndVisit;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.PropagandaScreeningDTO;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import org.nutz.dao.Cnd;
import org.nutz.lang.util.NutMap;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PatrolAndVisitExcelV2Generator extends BaseExcelGenerator {

    private String templateName;



    public PatrolAndVisitExcelV2Generator(){}

    public PatrolAndVisitExcelV2Generator(String templateName){
        this.templateName=templateName;
    }





    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {
        Map<String, Object> map= excelGenerateParam.getExcelConfig().getParam();
        SysUserService sysUserService =  NutzIocUtils.getBean(SysUserService.class);
        Sys_user sysUser=sysUserService.getUserById(SecurityUtil.getUserId());

        NutMap nutMap = NutMap.NEW();
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        Integer publicityRecord = 0;
        Integer searchRecord = 0;
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(Cnd.where(
                        Sqfw_formSummary_new::getType,"=", SupportBusinessTypeEnum.PROPAGANDA_OF_SAFE_CONSTRUCTION.getName())
                .and(Sqfw_formSummary_new::getFillDate,"between",new Object[]{map.get("fillStartDate") ,map.get("fillEndDate")  })
                .and(sysUser.getCommunity().equals("直属")?"county":"community" ,"=",sysUser.getCommunity().equals("直属")?sysUser.getCounty():sysUser.getCommunity()));
        List<Sqfw_formSummary_new> publicityRecordList = formSummaryNewList.stream().filter(s -> JSONObject.parseObject(s.getParamData(), PropagandaScreeningDTO.class).getType().equals(PatrolAndVisitEnum.A.getName()))
                .collect(Collectors.toList());
        for (Sqfw_formSummary_new sqfw_formSummary_new : publicityRecordList) {
            PropagandaScreeningDTO propagandaScreeningDTO=   JSONUtil.toBean(sqfw_formSummary_new.getParamData(),PropagandaScreeningDTO.class) ;
            publicityRecord = publicityRecord + Arrays.asList( propagandaScreeningDTO.getTheme().split(",")).size();
        }

        List<Sqfw_formSummary_new> searchRecordList = formSummaryNewList.stream().filter(s -> JSONObject.parseObject(s.getParamData(), PropagandaScreeningDTO.class).getType().equals(PatrolAndVisitEnum.B.getName()))
                .collect(Collectors.toList());
        for (Sqfw_formSummary_new sqfw_formSummary_new : searchRecordList) {
            PropagandaScreeningDTO propagandaScreeningDTO=   JSONUtil.toBean(sqfw_formSummary_new.getParamData(),PropagandaScreeningDTO.class) ;
            searchRecord = searchRecord + Arrays.asList( propagandaScreeningDTO.getTheme().split(",")).size();
        }
        List<Sqfw_formSummary_new> peaceAndCare = formSummaryNewList.stream().filter(s -> JSONObject.parseObject(s.getParamData(), PropagandaScreeningDTO.class).getType().equals(PatrolAndVisitEnum.C.getName()))
                .collect(Collectors.toList());
        nutMap.put("community", formSummaryNewList.get(0).getCommunity().equals("直属")?formSummaryNewList.get(0).getCounty():formSummaryNewList.get(0).getCommunity());
        nutMap.put("publicityRecord",publicityRecord );
        nutMap.put("searchRecord", searchRecord);
        nutMap.put("peaceAndCare", peaceAndCare.size());
        nutMap.put("num", "1");
        listMap.add(nutMap);
    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return PatrolAndVisitEnum.getFileName(Integer.parseInt(code));
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {

    }


}
